using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class pages_AdvancedSearch : PagedPage
{
    string mode = "";
    string requestString = "";
    string s="";
    int locId = 0;
    int quanlity = -1;
    decimal low_price = 0;
    decimal high_price = 0;

    #region properties
    public override string RequestString
    {
        get
        {
            requestString = "AdvancedSearch.aspx?page=";
            return requestString;
        }
    }
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (Session["keyword"]!=null) s = Session["keyword"].ToString();
         if( Session["mode"]!=null)mode = Session["mode"].ToString();
         if (Session["loc_id"]!=null) locId = int.Parse(Session["loc_id"].ToString());
         if (Session["quantity"]!=null) quanlity = int.Parse(Session["quantity"].ToString());
         if (Session["low_price"]!=null) low_price = decimal.Parse(Session["low_price"].ToString());
         if (Session["high_price"]!=null) high_price = decimal.Parse(Session["high_price"].ToString());
        //for paging  
         p = Request["page"];
         totals = 0;
         currentPage = GetPageNumber(p);

         if (!IsPostBack)
         {
             LoadData();
             Search(false);
         }
        //interface
         lnkArticle.CssClass = "search_nav";
         lnkTour.CssClass = "search_nav";
         lnkHotel.CssClass = "search_nav";
         lnkRestaurant.CssClass = "search_nav";
         lnkResort.CssClass = "search_nav";
         lnkSpot.CssClass = "search_nav";
         if (!IsPostBack)
         {
             lnkTour_Click(sender, e);             
         }
        
        //lnkResort
    }
    /// <summary>
    /// Load data into form
    /// </summary>
    public void LoadData()
    {
        Util util = new Util();
        LocationDAO daoLoc = new LocationDAO();        
        //location
        if (!IsPostBack)
        {
            System.Collections.Generic.IList<Location> locList = daoLoc.GetAllLocations();
            Location locAll = new Location();
            locAll.Name = "[All]";
            locAll.Id = -1;
            locList.Add(locAll);

            cmbLocation.DataSource = locList;
            cmbLocation.DataTextField = "Name";
            cmbLocation.DataValueField = "Id";
            cmbLocation.DataBind();
        }  
        //quanlity
        cmbQuanlity.Items.Clear();
        cmbQuanlity.Items.Add(new ListItem("[Not set]", "-1"));
        cmbQuanlity.Items.Add(new ListItem("1 Star", "1"));
        cmbQuanlity.Items.Add(new ListItem("2 Star", "2"));
        cmbQuanlity.Items.Add(new ListItem("3 Star", "3"));
        cmbQuanlity.Items.Add(new ListItem("4 Star", "4"));
        cmbQuanlity.Items.Add(new ListItem("5 Star", "5"));

        //page size box
        if (!IsPostBack)
        {
            
           cmbNumberOfItems.Items.Add(new ListItem("10 items", "10"));
           cmbNumberOfItems.Items.Add(new ListItem("20 items", "20"));
           cmbNumberOfItems.Items.Add(new ListItem("35 items", "35"));          
        }
    }
    protected void cmbTypeToSearch_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void lnkTour_Click(object sender, EventArgs e)
    {
        cmbQuanlity.Enabled = false;
        txtFromPrice.Enabled = true;
        txtToPrice.Enabled = true;
        cmbLocation.Enabled = true;
        Session["mode"] = "tour";
        lnkTour.CssClass = "search_nav active";
    }
    protected void lnkHotel_Click(object sender, EventArgs e)
    {
        txtFromPrice.Enabled = false;
        txtToPrice.Enabled = false;
        cmbQuanlity.Enabled = true;
        cmbLocation.Enabled = true;
        Session["mode"] = "hotel";
        lnkHotel.CssClass = "search_nav active";
    }
       protected void lnkResort_Click(object sender, EventArgs e)
    {
        txtFromPrice.Enabled = false;
        txtToPrice.Enabled = false;
        cmbQuanlity.Enabled = false;
        cmbLocation.Enabled = true;
        Session["mode"] = "resort";
        lnkResort.CssClass = "search_nav active";

    }
    protected void lnkArticle_Click(object sender, EventArgs e)
    {
        txtFromPrice.Enabled = false;
        txtToPrice.Enabled = false;
        cmbQuanlity.Enabled = false;
        cmbLocation.Enabled = false;
        Session["mode"] = "article";
        lnkArticle.CssClass = "search_nav active";
    }

    public void Search(bool postback)
    {
        pageSize = int.Parse(cmbNumberOfItems.SelectedValue);
        rptTours.Visible = false;
        rptPlaceHotel.Visible = false;
        rptArticle.Visible = false;
        rptSpot.Visible = false;
        switch (mode)
        {
            case "tour": rptTours.Visible = true; lnkTour.CssClass="search_nav active"; SearchTour(postback); break;
            case "hotel": rptPlaceHotel.Visible = true; lnkHotel.CssClass = "search_nav active"; ; SearchHotel(postback); break;
            case "restaurant": SearchRestaurant(postback); lnkRestaurant.CssClass = "search_nav active"; rptPlaceHotel.Visible = true; SearchRestaurant(postback); break;
            case "resort": rptPlaceHotel.Visible = true; SearchResort(postback); lnkResort.CssClass = "search_nav active"; break;
            case "article": rptArticle.Visible = true;SearchArticle(postback) ; lnkArticle.CssClass = "search_nav active"; break;
            case "spot": rptSpot.Visible = true; SearchSpot(postback); lnkSpot.CssClass = "search_nav active"; break;
        }
        //LoadData();
        lblPaging.Text = GetPageGrid(totals, pageSize, currentPage);
    }
/// <summary>
/// Search tours
/// </summary>
    public void SearchTour(bool postback)
    {
        try
        {
            TourDAO daoTour = new TourDAO();
             s ="%"+ txtSearch.Text+"%";
             locId =postback? int.Parse(cmbLocation.SelectedValue):int.Parse(Session["loc_id"].ToString());
             low_price = decimal.Parse(postback?txtFromPrice.Text:Session["low_price"].ToString());
             high_price = decimal.Parse(postback? txtToPrice.Text:Session["high_price"].ToString());
            if (high_price < low_price)
            {
                high_price = low_price;
                txtToPrice.Text = txtFromPrice.Text;
            }
            
            Session["mode"] = "tour";
            Session["loc_id"] = locId;
            Session["low_price"] = low_price;
            Session["high_price"] = high_price;
            Session["keyword"] = s;
            Session["quantity"] = cmbQuanlity.SelectedValue;
            if (locId > 0)
                rptTours.DataSource = daoTour.GetToursAdvByLoc(currentPage, pageSize, ref totals, s, low_price, high_price, false, locId, ServiceObject.Published);
            else rptTours.DataSource = daoTour.GetToursAdv(currentPage, pageSize, ref totals, s, low_price, high_price, false, ServiceObject.Published);
            rptTours.DataBind();
        }
        catch (Exception ex)
        {           
        }     
    }

    public void SearchHotel(bool postback)
    {
        try
        {
            PlaceDAO daoPlace = new PlaceDAO();
            s = "%" + txtSearch.Text + "%";
            locId = int.Parse(cmbLocation.SelectedValue);
            int quanlity = int.Parse(postback ? cmbQuanlity.SelectedValue : Session["quality"].ToString());
            if (locId > 0)
                rptPlaceHotel.DataSource = daoPlace.GetPlacesExtWithRank(currentPage, pageSize, ref totals, 1, locId, s, quanlity, ServiceObject.Published);
            else rptPlaceHotel.DataSource = daoPlace.GetPlacesByCategory(currentPage, pageSize, ref totals, 1, s, ServiceObject.Published);
            rptPlaceHotel.DataBind();
        }
        catch (Exception ex) {  }
    }
    public void SearchRestaurant(bool postback)
    {
        PlaceDAO daoPlace = new PlaceDAO();
        s = "%" + txtSearch.Text + "%";
        locId = int.Parse(postback?cmbLocation.SelectedValue:Session["loc_id"].ToString());
        if(locId>0)
        rptPlaceHotel.DataSource = daoPlace.GetPlacesExtByLoc(currentPage, pageSize, ref totals, 2, locId, s,false, ServiceObject.Published);
    else rptPlaceHotel.DataSource = daoPlace.GetPlacesByCategory(currentPage, pageSize, ref totals, 2, s, ServiceObject.Published);
        rptPlaceHotel.DataBind();
    }
    public void SearchResort(bool postback)
    {
        PlaceDAO daoPlace = new PlaceDAO();
        s = "%" + txtSearch.Text + "%";
        locId = int.Parse(postback ? cmbLocation.SelectedValue : Session["loc_id"].ToString());
        if(locId>0)
        rptPlaceHotel.DataSource = daoPlace.GetPlacesExtByLoc(currentPage, pageSize, ref totals, 3, locId, s, false, ServiceObject.Published);
        else
        rptPlaceHotel.DataSource = daoPlace.GetPlacesByCategory(currentPage, pageSize, ref totals, 3, s, ServiceObject.Published);
        rptPlaceHotel.DataBind();
    }
    public void SearchSpot(bool postback)
    {
        SpotDAO daoSpot = new SpotDAO();
        s = "%" + txtSearch.Text + "%";
        if (postback) locId = int.Parse(cmbLocation.SelectedValue);
        rptSpot.DataSource = daoSpot.GetSpotsExt(currentPage, pageSize, ref totals, locId, s, ServiceObject.Published);
        rptSpot.DataBind();
    }
    public void SearchArticle(bool postback)
    {
        ArticleDAO daoArticle = new ArticleDAO();
        s = "%" + txtSearch.Text + "%";
        rptArticle.DataSource = daoArticle.GetAllArticlesExt(pageSize, currentPage, ref totals, s, ServiceObject.Published);
        rptArticle.DataBind();

    }
    
    protected void lnkSearch_Click(object sender, EventArgs e)
    {
        Validate();
        if (!IsValid) return;     
        Search(true);   
    }   
    
    protected void lnkRestaurant_Click(object sender, EventArgs e)
    {
        txtFromPrice.Enabled = false;
        txtToPrice.Enabled = false;
        cmbQuanlity.Enabled = false;
        cmbLocation.Enabled = true;
        Session["mode"] = "restaurant";
        lnkRestaurant.CssClass = "search_nav active";
    }

    protected void lnkSpot_Click(object sender, EventArgs e)
    {
        txtFromPrice.Enabled = false;
        txtToPrice.Enabled = false;
        cmbQuanlity.Enabled = false;
        cmbLocation.Enabled = true;
        Session["mode"] = "spot";
        lnkSpot.CssClass = "search_nav active";
    }
}
